<template>
    <div>
        <van-nav-bar
			title="添加地址"
			left-text="返回"
			left-arrow
			@click-left="this.$router.go(-1)" />
        <van-address-edit
            :address-info="addressInfo"
            :area-list="areaList"
            show-delete
            show-set-default
            show-search-result
            :search-result="searchResult"
            @save="onSave"
            @delete="onDelete" />

        <!-- 网络请求加载状态 -->
		<van-overlay :show="isLoading" style="display: flex; justify-content: center; align-items: center;">
			<van-loading vertical>
				<template #icon>
					<van-icon name="star-o" size="30" />
				</template>
				加载中...
			</van-loading>
		</van-overlay>
    </div>
</template>

<script>
import { areaList } from "@vant/area-data";
import { showDialog, showFailToast, showSuccessToast } from 'vant';
export default {
    created() {
        this.infoid = this.$route.query.addressId;
        this.createdEdit(this.infoid);
    },
    data() {
        return {
            areaList,
            addressInfo: null,
            infoid: 0,
            isLoading: false,
        }
    },
    methods: {
        // 初始化编辑页
        async createdEdit(id){
            this.isLoading = true;
            // 没有id就返回
            if (!id) {
                this.$router.go(-1);
            }
            let res = await this.$api.AddressEdit({ busid: this.$cookies.get("User").id, id });
            // 异常不显示并且返回
            if (res.code == 1) {

                // 处理数据格式
                this.addressInfo = {
                    name: res.data.consignee,
                    tel: res.data.mobile,
                    areaCode: res.data.district,
                    addressDetail: res.data.address,
                    isDefault: res.data.status == 0? false: true
                }
            }
            showFailToast({
                message: res.msg
            });
            this.$router.go(-1);
            this.isLoading = false;
            
        },
        // 提交的时候
        async onSave(val){
            let data = {
                id: this.infoid,
                busid: this.$cookies.get("User").id,
                consignee: val.name,
                areaCode: val.areaCode,
                address: val.addressDetail,
                status: val.isDefault? 1: 0,
                mobile: val.tel
            }
            this.isLoading = true;
            let res = await this.$api.AddressAdd(data);
            
            if (res.code == 1) {
                // 提交成功
                showSuccessToast({
                    message: res.msg
                });
                this.$router.go(-1);
            }else{
                showFailToast({
                    message: res.msg
                });
            }
            this.isLoading = false;

        },
        // 删除的时候
        onDelete(){
            showDialog({
                title: '删除',
                message: '确认删除该地址吗',
                showCancelButton: true
            }).then(async() => {
                this.isLoading = true;
                let res = await this.$api.DelAddressEdit({ busid: this.$cookies.get("User").id, id: this.infoid });
                if (res.code == 1) {
                    // 删除成功
                    showSuccessToast({
                        message: res.msg
                    });
                    this.$router.go(-1);
                }else{
                    showFailToast({
                        message: res.msg
                    });
                }
                this.isLoading = false;
            })

            
        }
    },
}
</script>

<style>

</style>